"""
业务异常定义
===========
定义业务逻辑相关的异常
"""

from .base_exceptions import RedFireException, ResourceNotFoundException, ConflictException


class UserNotFoundException(ResourceNotFoundException):
    """用户未找到异常"""
    
    def __init__(self, user_id: str = None, username: str = None):
        if user_id:
            message = f"用户未找到: {user_id}"
            details = {'user_id': user_id}
        elif username:
            message = f"用户未找到: {username}"
            details = {'username': username}
        else:
            message = "用户未找到"
            details = {}
        
        super().__init__(
            message=message,
            resource_type="user"
        )
        self.details.update(details)


class UserAlreadyExistsException(ConflictException):
    """用户已存在异常"""
    
    def __init__(self, field: str, value: str):
        super().__init__(
            message=f"用户已存在: {field}={value}",
            resource_type="user"
        )
        self.details.update({
            'field': field,
            'value': value
        })


class InvalidCredentialsException(RedFireException):
    """无效凭据异常"""
    
    def __init__(self, message: str = "用户名或密码错误"):
        super().__init__(
            message=message,
            error_code="INVALID_CREDENTIALS"
        )


class AccountLockedException(RedFireException):
    """账户锁定异常"""
    
    def __init__(self, message: str = "账户已被锁定", locked_until: str = None):
        details = {}
        if locked_until:
            details['locked_until'] = locked_until
        
        super().__init__(
            message=message,
            error_code="ACCOUNT_LOCKED",
            details=details
        )


class InsufficientBalanceException(RedFireException):
    """余额不足异常"""
    
    def __init__(self, required_amount: str = None, available_amount: str = None):
        if required_amount and available_amount:
            message = f"余额不足，需要: {required_amount}，可用: {available_amount}"
            details = {
                'required_amount': required_amount,
                'available_amount': available_amount
            }
        else:
            message = "余额不足"
            details = {}
        
        super().__init__(
            message=message,
            error_code="INSUFFICIENT_BALANCE",
            details=details
        )


class InvalidTradeException(RedFireException):
    """无效交易异常"""
    
    def __init__(self, message: str = "无效的交易请求", trade_details: dict = None):
        details = trade_details or {}
        
        super().__init__(
            message=message,
            error_code="INVALID_TRADE",
            details=details
        )


class OrderNotFoundException(ResourceNotFoundException):
    """订单未找到异常"""
    
    def __init__(self, order_id: str):
        super().__init__(
            message=f"订单未找到: {order_id}",
            resource_type="order"
        )
        self.details.update({'order_id': order_id})


class OrderAlreadyCancelledException(RedFireException):
    """订单已取消异常"""
    
    def __init__(self, order_id: str):
        super().__init__(
            message=f"订单已被取消: {order_id}",
            error_code="ORDER_ALREADY_CANCELLED"
        )
        self.details.update({'order_id': order_id})


class OrderAlreadyFilledException(RedFireException):
    """订单已成交异常"""
    
    def __init__(self, order_id: str):
        super().__init__(
            message=f"订单已完全成交: {order_id}",
            error_code="ORDER_ALREADY_FILLED"
        )
        self.details.update({'order_id': order_id})


class MarketClosedException(RedFireException):
    """市场关闭异常"""
    
    def __init__(self, market: str, opens_at: str = None):
        details = {'market': market}
        if opens_at:
            details['opens_at'] = opens_at
            message = f"市场已关闭: {market}，开放时间: {opens_at}"
        else:
            message = f"市场已关闭: {market}"
        
        super().__init__(
            message=message,
            error_code="MARKET_CLOSED",
            details=details
        )


class PriceOutOfRangeException(RedFireException):
    """价格超出范围异常"""
    
    def __init__(self, price: str, min_price: str = None, max_price: str = None):
        details = {'price': price}
        if min_price:
            details['min_price'] = min_price
        if max_price:
            details['max_price'] = max_price
        
        message = f"价格超出允许范围: {price}"
        if min_price and max_price:
            message += f"，允许范围: {min_price} - {max_price}"
        
        super().__init__(
            message=message,
            error_code="PRICE_OUT_OF_RANGE",
            details=details
        )


class SymbolNotSupportedException(RedFireException):
    """交易对不支持异常"""
    
    def __init__(self, symbol: str):
        super().__init__(
            message=f"不支持的交易对: {symbol}",
            error_code="SYMBOL_NOT_SUPPORTED"
        )
        self.details.update({'symbol': symbol})
